package com.gesee.zyspps.activity;

import java.util.ArrayList;
import java.util.List;

import com.gesee.zys.R;
import com.gesee.zyspps.ZSApplication;
import com.gesee.zyspps.db.CmsPointsName;
import com.gesee.zyspps.db.CmsPointsNameDao.Properties;
import com.gesee.zyspps.utils.StringUtil;
import com.gesee.zyspps.utils.ToastUtil;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;

public class UpdateLeibieActivity extends BaseActivity
{
    
    private Spinner spinner;
    private EditText subCode,subName,parCode,parName,colorValue,et_mai,remark;
    private Button enter,cancle;
    private CmsPointsName oldCms;
    private ImageView come_back;
    
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_leibie);
        oldCms = (CmsPointsName) getIntent().getSerializableExtra("CmsPointsName");
        initView();
    }

    private void initView()
    {
        // TODO Auto-generated method stub
        come_back = (ImageView) findViewById(R.id.come_back);
        come_back.setOnClickListener(new OnClickListener()
        {
            
            @Override
            public void onClick(View v)
            {
                // TODO Auto-generated method stub
                UpdateLeibieActivity.this.finish();
            }
        });
        
        subCode = (EditText) findViewById(R.id.subCode);
        subCode.setText(oldCms.getSubCode());
        subName = (EditText) findViewById(R.id.subName);
        subName.setText(oldCms.getSubName());
        parCode = (EditText) findViewById(R.id.parCode);
        parCode.setText(oldCms.getParCode());
        parName = (EditText) findViewById(R.id.parName);
        parName.setText(oldCms.getParName());
        colorValue = (EditText) findViewById(R.id.colorValue);
        colorValue.setText(oldCms.getPointsColor());
        et_mai = (EditText) findViewById(R.id.et_mai);
        et_mai.setText(oldCms.getBuryType());
        remark = (EditText) findViewById(R.id.remark);
        remark.setText(oldCms.getRemark());
        enter = (Button) findViewById(R.id.enter);
        cancle = (Button) findViewById(R.id.cancle);
        
        spinner = (Spinner) findViewById(R.id.mai);
        
        List list = new ArrayList();
        list.add("管顶");
        list.add("管中");
        list.add("管底");
        ArrayAdapter adapter = new ArrayAdapter(this,
                android.R.layout.simple_spinner_dropdown_item, list);
        spinner.setAdapter(adapter);
        
        spinner.setOnItemSelectedListener(new OnItemSelectedListener()
        {

            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long arg3)
            {
                // TODO Auto-generated method stub
                if(position == 0){
                    et_mai.setText("管顶");
                }
                if(position == 1){
                    et_mai.setText("管中");
                }
                if(position == 2){
                    et_mai.setText("管底");
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0)
            {
                // TODO Auto-generated method stub
                
            }
        });
        
        if(oldCms.getBuryType().equals("管顶")){
            spinner.setSelection(0);
        }
        if(oldCms.getBuryType().equals("管中")){
            spinner.setSelection(1);
        }
        if(oldCms.getBuryType().equals("管底")){
            spinner.setSelection(2);
        }
        
        cancle.setOnClickListener(new OnClickListener()
        {
            
            @Override
            public void onClick(View v)
            {
                // TODO Auto-generated method stub
                UpdateLeibieActivity.this.finish();
            }
        });
        
        enter.setOnClickListener(new OnClickListener()
        {
            
            @Override
            public void onClick(View v)
            {
                // TODO Auto-generated method stub
                //cms.setId(id);
                oldCms.setSubCode(subCode.getText().toString().trim());
                oldCms.setSubName(subName.getText().toString().trim());
                oldCms.setParCode(parCode.getText().toString().trim());
                oldCms.setParName(parName.getText().toString().trim());
                oldCms.setPointsColor(colorValue.getText().toString().trim());
                oldCms.setRemark(remark.getText().toString().trim());
                oldCms.setBuryType(et_mai.getText().toString().trim());
                
                if(StringUtil.isEmpty(subCode.getText().toString().trim())||
                        StringUtil.isEmpty(subName.getText().toString().trim())||
                        StringUtil.isEmpty(parCode.getText().toString().trim())||
                        StringUtil.isEmpty(parName.getText().toString().trim())||
                        StringUtil.isEmpty(colorValue.getText().toString().trim())||
                        StringUtil.isEmpty(et_mai.getText().toString().trim())){
                    ToastUtil.show(UpdateLeibieActivity.this, "输入框必须填写!");
                }else{
                    ZSApplication.getDaoSession("zyspps-db").getCmsPointsNameDao().update(oldCms);
                    UpdateLeibieActivity.this.finish();
                }
                
            }
        });

        
    }
}
